package cn.mineserv.video.videoordersystem.controller;

import cn.mineserv.video.videoordersystem.model.*;
import cn.mineserv.video.videoordersystem.service.*;
import cn.mineserv.video.videoordersystem.utils.ModelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Qm && 爱吃蛋白的丶圆周率
 * @company 圆周率有限公司
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    UserService userService;
    @Resource
    MediaService mediaService;
    @Resource
    CategoryService categoryService;
    @Resource
    ReportService reportService;
    @Resource
    CommentService commentService;

    @GetMapping("/adminIndex")
    public String adminIndex(Model model,String keyWord){
        List<User> userList = userService.list();
        userList= userList.stream().filter(user -> user.getUserStatus() !=2 && user.getUserName().contains(keyWord!=null?keyWord:"")).collect(Collectors.toList());
        model.addAttribute("userList",userList);
        return "admin/adminIndex";
    }

    @PostMapping("/adminEditUser")
    public String adminEditUser(Model model,User user){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getUserId());
        boolean update = userService.update(user, updateWrapper);
        if (update){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return  "redirect:/admin/adminIndex";
    }

    @PostMapping("/adminBanUser")
    public String adminBanUser(Model model,int userId,int userStatus){
        User user = userService.getById(userId);
        user.setUserStatus(userStatus);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId);
        boolean update = userService.update(user, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminIndex";
    }

    @PostMapping("/adminDelUser")
    public String adminDelUser(Model model,int userId,int userStatus){
        User user = userService.getById(userId);
        user.setUserStatus(userStatus);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId);
        boolean update = userService.update(user, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminIndex";
    }

    @GetMapping("/adminManageVideo")
    public String adminManageVideo(Model model,String keyWord){
        List<Media> mediaList =  mediaService.list();
        mediaList=mediaList.stream().filter(media ->media.getMediaStatus()!=0 && media.getMediaName().contains(keyWord!=null?keyWord:"")).collect(Collectors.toList());
        List<Map<String, Object>> newList = new ArrayList<>();
        mediaList.forEach(media -> {
            Map<String, Object> map = ModelUtil.modelToMap(media);
            User user = userService.getById(media.getUserId());
            Category category = categoryService.getById(media.getCategoryId());
            if (user != null && category!= null) {
                map.put("userName", user.getUserName());
                map.put("mediaCategory", category.getCategoryName());
            }else {
                map.put("userName", "未知");
                map.put("mediaCategory", "未知");
            }
            newList.add(map);
        });
        List<Category> categoryList = categoryService.list();
        model.addAttribute("mediaList",newList);
        model.addAttribute("categoryList",categoryList);
        return "admin/adminManageVideo";
    }

    @PostMapping("/adminEditMedia")
    public String adminEditVideo(Model model,int mediaId,String mediaName,String mediaIntro,int categoryId,int mediaType){
        Media media = mediaService.getById(mediaId);
        media.setMediaName(mediaName);
        media.setMediaIntro(mediaIntro);
        media.setCategoryId(categoryId);
        media.setMediaType(mediaType);
        UpdateWrapper<Media> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("media_id",mediaId);
        boolean update = mediaService.update(media, updateWrapper);
        if (update){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return  "redirect:/admin/adminManageVideo";
    }

    @PostMapping("/adminBanMedia")
    public String adminBanVideo(Model model,int mediaId,int mediaStatus){
        Media media = mediaService.getById(mediaId);
        media.setMediaStatus(mediaStatus);
        UpdateWrapper<Media> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("media_id",mediaId);
        boolean update = mediaService.update(media, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminManageVideo";
    }

    @PostMapping("/adminDelMedia")
    public String adminDelVideo(Model model,int mediaId,int mediaStatus){
        Media media = mediaService.getById(mediaId);
        media.setMediaStatus(mediaStatus);
        UpdateWrapper<Media> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("media_id",mediaId);
        boolean update = mediaService.update(media, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminManageVideo";
    }

    @GetMapping("/adminManageComment")
    public String adminManageComment(Model model,String keyWord){
        List<Comment> commentList = commentService.list();
        commentList = commentList.stream().filter(comment -> comment.getCommentText().contains(keyWord!=null?keyWord:"")).collect(Collectors.toList());
        List<Map<String, Object>> newCommentList = new ArrayList<>();
        commentList.forEach(comment -> {
            Map<String, Object> map = ModelUtil.modelToMap(comment);
            User user = userService.getById(comment.getUserId());
            Media media = mediaService.getById(comment.getMediaId());
            if (user != null && media != null) {
                map.put("userName", user.getUserName());
                map.put("mediaName", media.getMediaName());
            }else {
                map.put("userName", "未知");
                map.put("mediaName", "未知");
            }
            newCommentList.add(map);
        });
        model.addAttribute("commentList",newCommentList);
        return "admin/adminManageComment";
    }

    @PostMapping("/adminReviewComment")
    public String adminReviewComment(Model model,int commentId,int commentStatus){
        Comment comment = commentService.getById(commentId);
        comment.setCommentStatus(commentStatus);
        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("comment_id",commentId);
        boolean update = commentService.update(comment, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminManageComment";
    }

    @GetMapping("/adminReview")
    public String adminReview(Model model,@RequestParam(required = false) String keyWord){
        QueryWrapper<Media> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_status",1).or().eq("media_status",3);
        if(keyWord != null){
            Arrays.stream(keyWord.split(" ")).collect(Collectors.toList()).forEach(key->{
                queryWrapper.like("media_name","%"+key+"%");
            });
        }
        List<Media> list = mediaService.list(queryWrapper);
        List<Map<String, Object>> reviewList = new ArrayList<>(list.size());
        list.forEach(media -> {
            Map<String, Object> map = ModelUtil.modelToMap(media);

            User user = userService.getById(media.getUserId());
            if(user != null){
                map.put("userName", user.getUserName());
            }
            reviewList.add(map);
        });
        model.addAttribute("reviewList",reviewList);
        model.addAttribute("enterChar","\n");
        return "admin/adminReview";
    }

    @PostMapping("/adminReview")
    public void adminReview(Model model,Integer mediaId,Integer mediaStatus){
        Media media = mediaService.getById(mediaId);
        media.setMediaStatus(mediaStatus);
        UpdateWrapper<Media> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("media_id",mediaId);
        mediaService.update(media, updateWrapper);
    }

    @GetMapping("/adminClassify")
    public String adminClassify(Model model,String keyWord){
        List<Category> categoryList = categoryService.list();
        categoryList = categoryList.stream().filter(category -> category.getCategoryName().contains(keyWord!=null?keyWord:"")).collect(Collectors.toList());
        model.addAttribute("categoryList",categoryList);
        System.out.println(categoryList);
        return "admin/adminClassify";
    }

    @PostMapping("/adminAddCategory")
    public String adminAddClassify(Model model,String categoryName,int categoryType){
        Category category = new Category();
        category.setCategoryName(categoryName);
        category.setCategoryType(categoryType);
        boolean save = categoryService.save(category);
        if (save){
            model.addAttribute("msg","添加成功");
        }else {
            model.addAttribute("msg","添加失败");
        }
        return "redirect:/admin/adminClassify";
    }

    @PostMapping("/adminEditCategory")
    public String adminEditClassify(Model model,int categoryId,String categoryName,int categoryType){
        Category category = categoryService.getById(categoryId);
        category.setCategoryName(categoryName);
        category.setCategoryType(categoryType);
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id",categoryId);
        boolean update = categoryService.update(category, updateWrapper);
        if (update){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return "redirect:/admin/adminClassify";
    }

    @PostMapping("/adminDelCategory")
    public String adminDelClassify(Model model,int categoryId){
//        Category category = categoryService.getById(categoryId);
//        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("category_id",categoryId);
//        boolean update =categoryService.remove(queryWrapper);
//        if (update){
//            model.addAttribute("msg","删除成功");
//        }else {
//            model.addAttribute("msg","删除失败");
//        }
        model.addAttribute("msg","删除成功");
        return "redirect:/admin/adminClassify";
    }

    @GetMapping("/adminProcessing")
    public String adminProcessing(Model model){
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_type",2);
        List<Report> reportedList = reportService.list(queryWrapper);
        List<Map<String, Object>> newReportedList = new ArrayList<>();
        reportedList.forEach(report -> {
            Map<String, Object> map = ModelUtil.modelToMap(report);

            User user = userService.getById(report.getUserId());
            if(user != null){
                map.put("userName", user.getUserName());
            }
            User reportedId = userService.getById(report.getReportedId());
            if (reportedId != null){
                map.put("reportedName", reportedId.getUserName());
            }
            newReportedList.add(map);
        });

        queryWrapper.clear();
        queryWrapper.eq("report_type",0).or().eq("report_type",1);
        List<Report> reportMediaList = reportService.list(queryWrapper);
        List<Map<String, Object>> newReportMediaList = new ArrayList<>();
        reportMediaList.forEach(report -> {
            Map<String, Object> map = ModelUtil.modelToMap(report);

            User user = userService.getById(report.getUserId());
            if(user != null){
                map.put("userName", user.getUserName());
            }
            Media media = mediaService.getById(report.getMediaId());
            if (media != null){
                map.put("mediaName", media.getMediaName());
                map.put("mediaCover", media.getMediaCover());
                map.put("mediaIntro", media.getMediaIntro());
                map.put("mediaDownload", media.getMediaDownload());
            }
            newReportMediaList.add(map);
        });
        model.addAttribute("reportedList",newReportedList);
        model.addAttribute("reportMediaList",newReportMediaList);
        return "admin/adminProcessing";
    }

    @PostMapping("/adminProcessReport")
    public String adminProcessUserConfirm(Model model,int reportId,int reportStatus){
        Report report = reportService.getById(reportId);
        report.setReportStatus(reportStatus);
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("report_id",reportId);
        boolean update = reportService.update(report, updateWrapper);
        if (update){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "redirect:/admin/adminProcessing";
    }


}
